layui.use(['form', 'layer'], function() {
	$ = layui.jquery;
	var form = layui.form,
		layer = layui.layer;

	//自定义验证规则
	form.verify({
		username: function(value) {
			if(value.length < 5) {
				return '昵称至少得5个字符啊';
			}
		},
		pass: [/(.+){6,12}$/, '密码必须6到12位'],
		// repass: function (value) {
		//     if ($('#L_pass').val() != $('#L_repass').val()) {
		//         return '两次密码不一致';
		//     }
		// },
		id_card: function(value) {
			if(value.length != 18) {
				return '请输入正确的18位身份证号码';
			}
		}
	});

	var url = dataport + "/MaterialInvoice/insert"; //请求数据url
	//post请求
	var postResult = (function() {
		$.post(url, $('#roleadd').serialize(),
			function(result) {
				if(result.code == '0') {
					layer.alert("增加成功", {
						icon: 6
					}, function() {
						// 获得frame索引
						var index = parent.layer.getFrameIndex(window.name);
						//关闭当前frame
						parent.layer.close(index);
					});
				} else{
					layer.alert("服务器繁忙，稍后再试", {
						icon: 5
					}, function() {
						// 获得frame索引
						var index = parent.layer.getFrameIndex(window.name);
						//关闭当前frame
						parent.layer.close(index);
					});
				}
			});
	})

	//监听提交
	form.on('submit(add)', function(data) {
		console.log(data);
		//发异步，把数据提交给php
		postResult();
		return false;
	});
});